<?php

namespace App\Models;

class Member extends BaseModel
{
    public function scopeWhenName($q, $name)
    {
        $q->when($name, function ($q) use ($name) {
            $q->whereHas('user', function ($q) use ($name) {
                $q->where('username', $name);
            });
        });
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function violations()
    {
        return $this->hasMany(Violation::class);
    }

    public function agency()
    {
        return $this->belongsTo(Agency::class);
    }

    public function buys()
    {
        return $this->hasMany(Buy::class);
    }

    public function boughts()
    {
        return $this->hasMany(Buy::class)->where('state', 'done');
    }

    public function sells()
    {
        return $this->hasMany(Sell::class);
    }

    public function solds()
    {
        return $this->hasMany(Sell::class)->where('state', 'done');
    }
}
